﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using HoangHuy.data;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using ERP.form;
using HoangHuy.Utils;


namespace HoangHuy.form
{
    public partial class frmDanhSachMatHang : DevExpress.XtraEditors.XtraForm
    {
        public frmDanhSachMatHang()
        {
            InitializeComponent();
        }

        public MobileDataDataContext context = new MobileDataDataContext();
        public int currentRow = 0;
        public DataTable dataInfo = null;
        public string tenHangTim = string.Empty;
        public int[] arrSelectedRows;
        public int[] arrIdSelected;

        public void loadrepLoaiHang()
        {
            repLoaiHang.DataSource = context.LoaiHangs;
            repLoaiHang.DisplayMember = "tenLoaiHang";
            repLoaiHang.ValueMember = "id";
            repLoaiHang.View.Columns["tenLoaiHang"].Caption = "Tên Loại Hàng";
            repLoaiHang.View.Columns["id"].Visible = false;
        }

        public void loadDataGridView(string tenHang)
        {
            dataInfo = Utils.Utils.LINQToDataTable(context.matHangs.Where(q =>q.tenHang.Contains(tenHang)));
            grdMatHang.DataSource = dataInfo;
        }

        public void convertRowSelectedToIdSelected()
        {
            arrIdSelected = new int[arrSelectedRows.Length];
            for (int i = 0; i < arrSelectedRows.Length; i++)
            {
                arrIdSelected[i] = int.Parse(grvMatHang.GetRowCellValue(arrSelectedRows[i], colsId).ToString());
            }
        }

        private void frmDanhSachMatHang_Load(object sender, EventArgs e)
        {
            loadrepLoaiHang();
            loadDataGridView(string.Empty);
        }

        private void eNhapHang_click(object sender, EventArgs e)
        {
            if (arrSelectedRows.Length == 1)
            {
                string tenHang = string.Empty;
                int id = 0;
                id = int.Parse(dataInfo.Rows[currentRow]["id"].ToString());
                tenHang = dataInfo.Rows[currentRow]["tenHang"].ToString();
                frmNhapXuatHang frm = new frmNhapXuatHang((int)HoangHuy.Utils.enumConstant.LoaiNhapXuat.Nhap, tenHang);
                DialogResult result = frm.ShowDialog();

                if (result == DialogResult.Yes)
                {
                    int soLuong = frm.soLuong;
                    var matHang = context.matHangs.Where(q => q.id.Equals(id)).SingleOrDefault();
                    matHang.soLuong += soLuong;
                    context.SubmitChanges();
                    loadDataGridView(tenHangTim);
                }
            }
            else
            {
                convertRowSelectedToIdSelected();
                frmNhapXuatHangSoLuong frm = new frmNhapXuatHangSoLuong((int)HoangHuy.Utils.enumConstant.LoaiNhapXuat.Nhap, arrIdSelected);
                DialogResult digResult = frm.ShowDialog();

                if (digResult == DialogResult.Yes)
                {
                    int id = 0;
                    int soluong = 0;
                    for (int i = 0; i < frm.grvData.RowCount; i++)
                    {
                        id = int.Parse(frm.grvData.GetRowCellValue(i, "id").ToString());
                        soluong = int.Parse(frm.grvData.GetRowCellValue(i, "soLuong").ToString());
                        var mathang = context.matHangs.Where(q => q.id.Equals(id)).Single();
                        mathang.soLuong += soluong;
                    }
                    context.SubmitChanges();
                }
                loadDataGridView(tenHangTim);
            }
        }

        private void eXuatHang_click(object sender, EventArgs e)
        {
            if (arrSelectedRows.Length == 1)
            {
                string tenHang = string.Empty;
                int id = 0;
                tenHang = grvMatHang.GetRowCellValue(currentRow, colstenHang).ToString();
                id = int.Parse(grvMatHang.GetRowCellValue(currentRow, colsId).ToString());
                frmNhapXuatHang frm = new frmNhapXuatHang((int)HoangHuy.Utils.enumConstant.LoaiNhapXuat.Xuat, tenHang);
                DialogResult result = frm.ShowDialog();

                if (result == DialogResult.Yes)
                {
                    int soLuong = frm.soLuong;
                    var matHang = context.matHangs.Where(q => q.id.Equals(id)).SingleOrDefault();
                    if (matHang.soLuong < soLuong)
                    {
                        MessageBox.Show("số lượng hàng không đủ để xuất..!");
                    }
                    else
                    {
                        matHang.soLuong -= soLuong;
                        context.SubmitChanges();
                        loadDataGridView(tenHangTim);
                    }
                }
            }
            else // xuất hàng nhiều mặt hàng 1 lúc.
            {
                convertRowSelectedToIdSelected();
                frmNhapXuatHangSoLuong frm = new frmNhapXuatHangSoLuong((int)HoangHuy.Utils.enumConstant.LoaiNhapXuat.Xuat, arrIdSelected);
                DialogResult digResult = frm.ShowDialog();

                if (digResult == DialogResult.Yes)
                {
                    int id = 0;
                    int soluong = 0;
                    for (int i = 0; i < frm.grvData.RowCount; i++)
                    {
                        id = int.Parse(frm.grvData.GetRowCellValue(i, "id").ToString());
                        soluong = int.Parse(frm.grvData.GetRowCellValue(i, "soLuong").ToString());
                        var mathang = context.matHangs.Where(q => q.id.Equals(id)).Single();
                        mathang.soLuong -= soluong;
                    }
                    context.SubmitChanges();
                }
                loadDataGridView(tenHangTim);
            }
        }

        private void eSuaThongTin_click(object sender, EventArgs e)
        {
            // Từ currentRow lấy id. 
            // mở form nhập hàng.
            // load lại gridview.
        }

        private void gridView_RowClicked(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                arrSelectedRows = grvMatHang.GetSelectedRows();
                currentRow = e.RowHandle;
                ContextMenu m = new ContextMenu();
                m.MenuItems.Add("Nhập Hàng", new EventHandler(eNhapHang_click));
                m.MenuItems.Add("Xuất Hàng", new EventHandler(eXuatHang_click));
                m.MenuItems.Add("Sửa Thông Tin", new EventHandler(eSuaThongTin_click));
                GridView view = sender as GridView;
                GridHitInfo hitInfo = view.CalcHitInfo(e.Location);
                if (hitInfo.InRow)
                {
                    view.FocusedRowHandle = hitInfo.RowHandle;
                    m.Show(view.GridControl, e.Location);
                }
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            tenHangTim = txtTenHang.Text;
            loadDataGridView(tenHangTim);
        }

    }
}